/*
 * Copyright (c) 2023 IUV Inc. All rights reserved.
 * 项目名称：IUV TXY
 * 文件名：DemoBaikeStatusServiceImpl.java
 * Date：23-10-10 下午2:09
 * Author：Will
 */

package com.iuv.baike.service.impl;

import com.aliyun.dytnsapi20200217.models.PhoneNumberStatusForPublicResponse;
import com.aliyun.dytnsapi20200217.models.PhoneNumberStatusForPublicResponseBody;
import com.iuv.baike.domain.DemoBaikeStatusTbl;
import com.iuv.baike.dto.StatusInfo;
import com.iuv.baike.repository.DemoBaikeStatusTblRepository;
import com.iuv.baike.service.DemoBaikeStatusService;
import com.iuv.baike.util.HaoMaBaiKeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * DemoBaikeStatusServiceImpl
 * 号码百科号码状态例子-业务实现类
 *
 * @author Will
 * @date 13:50
 */
@Service("demoBaikeStatusService")
public class DemoBaikeStatusServiceImpl implements DemoBaikeStatusService {
    @Autowired
    private DemoBaikeStatusTblRepository demoBaikeStatusTblRepository;


    @Override
    public StatusInfo queryOverYun(String mobile) {


        PhoneNumberStatusForPublicResponse phoneNumberStatusForPublicResponse =HaoMaBaiKeUtil.queryOnlineTime(mobile);
        if(phoneNumberStatusForPublicResponse!=null)
        {
            PhoneNumberStatusForPublicResponseBody body=phoneNumberStatusForPublicResponse.getBody();
            if("OK".equalsIgnoreCase(body.getCode()))
            {
                PhoneNumberStatusForPublicResponseBody.PhoneNumberStatusForPublicResponseBodyData data=body.getData();
                if(data!=null) {
                    DemoBaikeStatusTbl demoBaikeStatusTbl=new DemoBaikeStatusTbl();
                    String status =realStatus(data.getStatus());
                    String carrierCode =realCarrierCode(data.getCarrier());
                    demoBaikeStatusTbl.setCarrier(carrierCode);
                    demoBaikeStatusTbl.setMobile(mobile);
                    demoBaikeStatusTbl.setStatus(status);
                    Date now = new Date();
                    demoBaikeStatusTbl.setCreateTime(now);
                    demoBaikeStatusTbl.setLastUpdateTime(now);
                    demoBaikeStatusTblRepository.save(demoBaikeStatusTbl);
                    return changeTo(demoBaikeStatusTbl);
                }
            }
        }
        return null;
    }

    @Override
    public StatusInfo queryByMobile(String moblie) {
        if(moblie!=null)
        {
            DemoBaikeStatusTbl demoBaikeOnlineHourTbl= demoBaikeStatusTblRepository.queryTopByMobile(moblie);
            if(demoBaikeOnlineHourTbl!=null)
            {
                return changeTo(demoBaikeOnlineHourTbl);
            }
        }
        return null;
    }

    private StatusInfo changeTo(DemoBaikeStatusTbl demoBaikeStatusTbl)
    {
        return new StatusInfo(demoBaikeStatusTbl.getMobile(),demoBaikeStatusTbl.getStatus(),demoBaikeStatusTbl.getCarrier());
    }

    private String realStatus(String status)
    {
        String realStatus="";
        if(status!=null)
        {
            if("NORMAL".equalsIgnoreCase(status))
            {
                realStatus="正常";
            }else if("SHUTDOWN".equalsIgnoreCase(status))
            {
                realStatus="停机";
            }else if("POWER_OFF".equalsIgnoreCase(status))
            {
                realStatus="关机";
            }else if("NOT_EXIST".equalsIgnoreCase(status))
            {
                realStatus="空号";
            }else if("SUSPECTED_POWER_OFF".equalsIgnoreCase(status))
            {
                realStatus="疑似关机";
            }else if("BUSY".equalsIgnoreCase(status))
            {
                realStatus="忙";
            }else if("UNKNOWN".equalsIgnoreCase(status))
            {
                realStatus="未知";
            }
        }
        return realStatus;
    }

    private String realCarrierCode(String carrierCode)
    {
        String realCarrierCode="";
        if(carrierCode!=null)
        {
            if("CMCC".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国移动";
            }else if("CUCC".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国联通";
            }else if("CTCC".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国电信";
            }else if("CBN".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="中国广电";
            }
        }
        return realCarrierCode;
    }
}
